package com.example.system.service.impl;


import cn.dev33.satoken.stp.StpUtil;
import com.example.common.constant.SystemConstant;
import com.example.common.response.R;
import com.example.common.utils.GzipUtils;
import com.example.system.domain.Friend;
import com.example.system.domain.bo.FriendBo;
import com.example.system.domain.bo.PageBo;
import com.example.system.domain.vo.FriendVo;
import com.example.system.domain.vo.PageVo;
import com.example.system.domain.vo.UserVo;
import com.example.system.mapper.FriendMapper;
import com.example.system.mapper.UserMapper;
import com.example.system.service.DictService;
import com.example.system.service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class FriendServiceImpl implements FriendService {
    @Autowired
    private FriendMapper friendMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DictService dictService;
    @Autowired
    private GzipUtils gzipUtils;

    @Override
    public PageVo<FriendVo> getListPage(PageBo pageBo) {
        List<FriendVo> list = this.getList();
        return new PageVo<>(pageBo, list);
    }

    @Override
    public List<FriendVo> getList() {
        int userId = this.getUserIdByAccount();
        return friendMapper.getList(userId).stream().peek(this::imageToByte).collect(Collectors.toList());
    }

    @Override
    public PageVo<FriendVo> getListSearch(String username, PageBo pageBo) {
        List<FriendVo> list = this.getList();
        list = list.stream().filter(o -> (o.getNotes() != null && o.getNotes().contains(username)) || o.getName().contains(username) || o.getUserB().contains(username)).collect(Collectors.toList());
        return new PageVo<>(pageBo, list);
    }

    @Override
    public List<FriendVo> searchAll(String username) {
        List<UserVo> userList = userMapper.selectUserLists(username);
        List<FriendVo> friendList = userList.stream().map(m -> {
            FriendVo friend1 = new FriendVo();
            friend1.setUserB(m.getUsername());
            friend1.setNotes(m.getName());
            friend1.setImage(m.getImage());
            return friend1;
        }).collect(Collectors.toList());
        return friendList;

    }

    @Override
    public PageVo<FriendVo> getApplicationList(PageBo pageBo) {
        int userId = this.getUserIdByAccount();
        List<FriendVo> friendList = friendMapper.getApplicationList(userId).stream().peek(this::imageToByte).collect(Collectors.toList());
        return new PageVo<>(pageBo, friendList);
    }

    @Override
    public PageVo<FriendVo> getApplicationLogList(PageBo pageBo) {
        int userId = this.getUserIdByAccount();
        List<FriendVo> friendList = friendMapper.getApplicationLogList(userId).stream().peek(this::imageToByte).collect(Collectors.toList());
        return new PageVo<>(pageBo, friendList);
    }

    // status 2 同意 3 拒绝
    @Override
    public R<String> changeStatus(FriendBo bo) {
        friendMapper.updateByUserIdAAndB(bo);
        if ("2".equals(bo.getStatus())) {
            Friend friend = new Friend();
            friend.setUserA(bo.getUserB());
            friend.setUserIdA(bo.getUserIdB());
            friend.setUserB(bo.getUserA());
            friend.setUserIdB(bo.getUserIdA());
            friend.setStatus(SystemConstant.friend_status_normal);
            friend.setNotes("");
            friendMapper.insertFriend(friend);
        }
        return R.ok("操作成功");
    }

    public int getUserIdByAccount() {
        String account = (String) StpUtil.getLoginId();
        return userMapper.getUserByAccount(account).getUserId();
    }

    public void imageToByte(FriendVo vo) {
        Map<String, byte[]> map = new HashMap<>();
        map.put("byte", gzipUtils.compress(vo.getImage()));
        vo.setImage_byte(map);
        vo.setImage("");
    }
}
